fun main() {
    var node5=Node("5",null)
    var node4=Node("4",node5)
    var node3=Node("3",node4)
    var node2=Node("2",node3)
    var node1=Node("1",node2)
    //打印输出Node(data=1, next=Node(data=2, next=Node(data=3, next=Node(data=4, next=Node(data=5, next=null)))))
    println(node1)
    var reverseList = reverseList(node1)
    //打印输出Node(data=5, next=Node(data=4, next=Node(data=3, next=Node(data=2, next=Node(data=1, next=null)))))
    println(reverseList)
}
data class Node(var data: String, var next: Node?)
/*
* 下面是链表遍历基本写法
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
    // 反转 prev 和 curr 之间的指针
    prev = curr;
    curr = curr.next;
}
*解题思路:遍历列表,将指向下一个的指针指向上一个
* */
fun reverseList(head: Node): Node {
    var prev: Node? = null
    var curr: Node? = head
    while (curr != null) {
        val cnext: Node? = curr.next
        if (prev == null) {
            curr.next = null
        } else {
            curr.next = prev
        }
        prev = curr
        curr = cnext
    }
    return prev!!
}